# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib import auth
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
from forms import CategoryForm
from forms import CodeForm
from models import Category
from models import UserCode
import datetime, random, sha
from django.core.mail import send_mail


def index(request):
	user = request.user
	return render_to_response('index.html', locals())
	
@login_required	
def new_category(request):
	categories = Category.objects.all()
	if request.method == 'POST':
		form = CategoryForm(request.POST)
		if form.is_valid():
			form.cleaned_data
			new_category = form.save()
			process_code = True	
		else:
			process_code = False	
	else:
		form = CategoryForm()
	return render_to_response('new_category.html', locals())	
	
@login_required	
def new_code(request):
	codes = UserCode.objects.all()
	if request.method == 'POST':
		instance = UserCode(user=request.user)
		form = CodeForm(request.POST, instance=instance)		
		if form.is_valid():
			form.cleaned_data
			new_code = form.save()
	else:
		form = CodeForm()
	return render_to_response('new_code.html', locals())	


@login_required
def show_codes(request):
	codes = UserCode.objects.filter(user=request.user).order_by('updated')
	return render_to_response('codes.html', locals())	


def show_categories(request):
	categories = Category.objects.all().order_by('updated')
	return render_to_response('categories.html', locals())	
	
@login_required
def show_code(request, code_id):
	code = UserCode.objects.get(pk=code_id)
	return render_to_response('show_code.html', locals())	

@login_required
def show_category(request, category_id):
	category = Category.objects.get(pk=category_id)
	return render_to_response('show_category.html', locals())	


@login_required
def edit_code(request, code_id):
	code = UserCode.objects.get(pk=code_id)
	if request.method == 'POST':
		form = CodeForm(request.POST, instance=code)
		if form.is_valid():
			form.cleaned_data
			c = form.save()
			return HttpResponseRedirect("/code/show/%s/" % str(c.pk))
	else:		
		form = CodeForm(instance=code)
	return render_to_response('edit_code.html', locals())	

@login_required
def edit_category(request, category_id):
	category = Category.objects.get(pk=category_id)
	if request.method == 'POST':
		form = CategoryForm(request.POST, instance=category)
		if form.is_valid():
			form.cleaned_data
			c = form.save()
			return HttpResponseRedirect("/category/show/%s/" % str(c.pk))
	else:		
		form = CategoryForm(instance=category)
	return render_to_response('edit_category.html', locals())	


@login_required
def delete_code(request, code_id):
	code = UserCode.objects.get(pk=code_id)
	if request.method == 'POST':
		if 'yesButton' in request.POST.keys():
			code.delete()
		return HttpResponseRedirect("/code/all/")
	else:
		return render_to_response('delete_code.html', locals())	

@login_required
def delete_category(request, category_id):
	category = Category.objects.get(pk=category_id)
	codes = category.codes.all()
	if request.method == 'POST':
		if 'yesButton' in request.POST.keys():
			category.delete()
		return HttpResponseRedirect("/category/all/")
	else:
		return render_to_response('delete_category.html', locals())	


#
# Authentication Stuff
#
	
def login(request):
	return render_to_response('login.html')
	
def do_login(request):
	if request.method == 'POST':
		f_username = request.POST['f_username']
		f_password = request.POST['f_password']
		user = auth.authenticate(username=f_username, password=f_password)
		if user is not None and user.is_active:
			# Correct password, and the user is marked "active"
			auth.login(request, user)
			# Redirect to the main
			return HttpResponseRedirect("/")
		
	else:
		# Show an error page
		return HttpResponseRedirect("/login/")
	# request should be a POST    	

	return HttpResponseRedirect("/")

@login_required
def do_logout(request):
    auth.logout(request)
    # Redirect to the main
    return HttpResponseRedirect("/")


def register(request):
	if request.method == 'POST':
		form = UserCreationForm(request.POST)
		form.save()
		return HttpResponseRedirect("/")
	else:
		form = UserCreationForm()
	return render_to_response('register.html', {'form': form})

